/**
  ******************************************************************************
  * @file           : oled.cpp
  * @author         : admin
  * @brief          : None
  * @attention      : None
  * @date           : 2024/10/9
  ******************************************************************************
  */

#include "oled.h"

oled::oled()  : u8g2_12864(U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, SCL, SDA, U8X8_PIN_NONE)){
}
oled::~oled() {
    u8g2_12864 = NULL;
}
void oled::init() {
    u8g2_12864.setI2CAddress(OLEDADDR<<1);
    u8g2_12864.begin();
    u8g2_12864.enableUTF8Print();
    u8g2_12864.setFont(u8g2_font_wqy16_t_gb2312);
//    u8g2_12864.setFont(u8g2_font_t0_16_tr);
    u8g2_12864.setFontDirection(0);

}

void oled::setCursorLine(int number) {
    CursorLine = number;
}
void oled::fillInLine(int line,int Screen){
    if (line <=4 && line >=1){
        u8g2_12864.setDrawColor(Screen);
        u8g2_12864.drawBox(0, line*16 -16, 128, 16);
        u8g2_12864.setDrawColor(1);
        u8g2_12864.sendBuffer();
    }

}
void oled::setCursor(int x, int y) {
    x = x > 127? 127:x;
    x = x < 0 ? 0 : x;
    y = y > 63? 63:y;
    y = y < 0 ? 0 : y;
    y = y+13;
    u8g2_12864.setCursor(x, y);
}
void oled::fillScreen(int Screen) {
    u8g2_12864.setDrawColor(Screen);        // 设置绘制颜色为背景
    u8g2_12864.drawBox(0, 0, 128, 64); // 绘制与第二行相同高度的矩形
    u8g2_12864.sendBuffer();
    u8g2_12864.setDrawColor(1);        // 设置绘制颜色为背景
}
void oled::setRotaion(eROTATION e) {
    if (e == eROTATION_0){
        u8g2_12864.setDisplayRotation(U8G2_R2);
    }else if(e == eROTATION_180){
        u8g2_12864.setDisplayRotation(U8G2_R0);
    }
}

void oled::point(int x, int y){
    u8g2_12864.drawPixel(x, y);
    u8g2_12864.sendBuffer();
}
void oled::line(int x1, int y1, int x2, int y2) {
    u8g2_12864.drawLine(x1, y1, x2, y2);
    u8g2_12864.sendBuffer();
}

void oled::fillCircle(int x,int y,int radius){
    u8g2_12864.drawDisc(x,y,radius);
    u8g2_12864.sendBuffer();
}
void oled::drawCircle(int x,int y,int radius){
    u8g2_12864.drawCircle(x,y,radius);
    u8g2_12864.sendBuffer();
}
void oled::rect(int x, int y, int w, int h, bool isFill) {
    if (isFill){
        u8g2_12864.drawBox(x,y,w,h);
    }else{
        u8g2_12864.drawFrame(x,y,w,h);
    }
    u8g2_12864.sendBuffer();
}

oled display;